<template>
  <el-dialog :visible.sync="visible" v-dialogDrag :title="title" :close-on-click-modal="false" class="tl-dialog"
    width="1000px" @open="open" @close="close" :append-to-body="true" :modal-append-to-body="true">
    <el-form class="tl-form" ref="form" :model="model" :rules="rules" label-width="150px" :disabled="disableEditSubmit">
      <el-row>
        <el-col :span="12">
          <el-form-item label="任命类型" prop="appointmenttype" :rules="rules.validateRequire">
            <el-select v-model="model.appointmenttype" :disabled="true">
              <el-option v-for="item in codeModel.appointmenttypeList" :key="item.code" :label="item.name"
                :value="item.code" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="排序码" prop="sortcode">
            <el-input-number v-model="model.sortcode" controls-position="right" :step="1" :min="0" style="width:100%"></el-input-number>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="姓名" prop="usercode" :rules="rules.validateRequire">
            <tl-ref-user v-model="model.usercode" :initDisplayData.sync="model.username" dataType="code"
              :singleSelect="true">
            </tl-ref-user>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="工号" prop="usercode" :rules="rules.validateRequire">
            <el-input v-model="model.usercode" :disabled="true"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="工作组名称" prop="workgroup">
            <el-input :disabled="true" v-model="model.workgroup"></el-input>
            <!-- <el-select v-model="model.workgroup" filterable clearable @change="clear()">
              <el-option v-for="item in codeModel.workgroupList" :key="item.code" :label="item.name"
                :value="item.code" />
            </el-select> -->
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="项目角色" prop="projectrole" :rules="rules.validateRequire">
            <el-select v-model="model.projectrole" filterable clearable @click.native="judgeGroup()">
              <el-option v-for="item in projectRoleList" :key="item.code" :label="item.name"
                :value="item.code" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <!-- <el-col :span="12">
          <el-form-item label="职务" prop="position" :rules="rules.validateRequire">
            <el-input v-model="model.position"></el-input>
          </el-form-item>
        </el-col> -->
        <el-col :span="24">
          <el-form-item label="职责" prop="duty" :rules="rules.validateRequire">
            <el-input v-model="model.duty"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="备注" prop="description">
            <el-input v-model="model.description" type="textarea" :rows="4"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :type="disableEditSubmit === false ? 'text' : 'primary'" @click="doCancelEdit">取消</el-button>
      <el-button type="primary" @click="doSaveAndCopy" :loading="saveLoading">保存并复制</el-button>
      <el-button type="primary" @click="doSubmitEdit" :loading="saveLoading" v-if="disableEditSubmit === false">确认
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
/** mixins */
import { mixin_edit } from '@tapui/mixins/mixin_edit'
import { mixin_page_fields_custom } from '@/views/gpm/gpmProjectInfo/mixin/mixin_page_fields_custom.js'
/** 子组件 */
/** 工具类 */
import { putAction, getAction, postAction, deleteAction } from '@tapui/utils/http'
/** VUE配置 */
export default {
  mixins: [mixin_edit, mixin_page_fields_custom],
  components: {},
  data() {
    return {
      moduleTitle: '项目任命-项目组成员',
      urlPrefix: '/sd/projectTeam/',
      url: {
        // 额外的url
        queryCodeList: '/sd/projectTeam/dataCodeList'
      },
      canEditWorkGroup: false,
      projectRoleList: []
    }
  },
  props: {
    parentId: {
      type: [Number, String],
      default: ''
    },
    projectId: {
      type: [Number, String],
      default: ''
    },
    projectCode: {
      type: [Number, String],
      default: ''
    },
    teamType: {
      type: [Number],
      default: 0
    },
    teamName: {
      type: String,
      default: ''
    }
  },
  methods: {
    /** 查询编码 */
    doQueryCode() {
      if (this.url.queryCodeList) {
        getAction(this.url.queryCodeList, {}).then((res) => {
          if (res.success) {
            this.codeModel = res.result
            if (this.codeModel.projectroleList && this.codeModel.projectroleList.length > 0) {
              this.projectRoleList = this.codeModel.projectroleList.filter(item => (item.code === 'groupworker' || item.code === 'groupleader'))
            }
          } else {
            this.$tap.alert(res.message, 'warning')
          }
        })
      }
    },
    assignDefaultValue() {
      if (this.isAdd) {
        this.$set(this.model, 'appointdate', new Date())
        this.$set(this.model, 'applydate', new Date())
      }
      if (this.model.workgroup !== '') {
        this.canEditWorkGroup = true
      }
    },
    loaded() {
      // 页面加载完毕执行
      if (this.isAdd) {
        this.$set(this.model, 'projectcode', this.projectCode)
        this.$set(this.model, 'projectid', this.projectId)
        this.$set(this.model, 'appointmentid', this.parentId)
        this.$set(this.model, 'appointmenttype', '1')
        this.$set(this.model, 'teamid', this.teamType)
        this.$set(this.model, 'workgroup', this.teamName)
      }
    },
    doSaveAndCopy() {
      this.saveLoading = true
      this.$set(this.model, 'appointmentid', this.parentId)
      let isvalidate = true
      this.$refs.form.validate(valid => {
        if (valid) {
        } else {
          isvalidate = false
        }
      })
      if (!isvalidate) {
        this.saveLoading = false
        return false
      }
      // 增加唯一单号
      postAction(this.url.add + '?billuid=' + this.uid, this.model)
        .then(res => {
          if (res.success) {
            this.$tap.message('添加成功,请继续添加', 'success')
            this.model = res.result
            this.$delete(this.model, 'id')
            this.$delete(this.model, 'usercode')
            this.$delete(this.model, 'username')
            this.$set(this.model, 'appointmenttype', '1')
            // this.visible = false
            this.$emit('refresh')
          } else {
            this.$tap.alert(res.message, 'warning')
          }
          this.afterSubmit()
          this.saveLoading = false
        })
        .catch(err => {
          this.saveLoading = false
        })
    },
    // 项目角色点击事件
    judgeGroup() {
      //   if (!this.model.workgroup) {
      //     this.$tap.alert('请先选择工作组名称', 'warning')
      //     this.$set(this.model, 'projectrole', '')
      //     return
      //   }
      //   if (this.model.workgroup !== 'manager') {
      //     this.codeModel.projectroleList.some((item, i) => {
      //       if (item.code === 'pm') {
      //         this.codeModel.projectroleList.splice(i, 1)
      //       }
      //       if (item.code === 'sponsor') {
      //         this.codeModel.projectroleList.splice(i, 1)
      //       }
      //       if (item.code === 'owner') {
      //         this.codeModel.projectroleList.splice(i, 1)
      //       }
      //     })
      //     console.log(this.codeModel)
      //   }else{
      //         this.codeModel.projectroleList.add("")
      //         this.codeModel.projectroleList.add(i, 1)
      //         this.codeModel.projectroleList.add(i, 1)
      //   }
    },
    // 清空项目角色
    clear() {
      if (this.model.projectrole) {
        this.$set(this.model, 'projectrole', '')
      }
    },
    open() {
      this.model = {}
      this.$nextTick(async () => {
        await this.getFieldsCustomSetting('sdProjectTeamEdit', 'el-form')
        this.$refs.form.clearValidate()
        if (this.isAdd === true) {
          this.title = '添加' + this.moduleTitle
          // 需要添加重复判断
          if (this.$store.getters.param.isAvoidRepeat === true) {
            this.queryBillUid()
          }
          if (this.isQueryInitData === true) {
            // 后台进行数据初始化
            this.queryInitData().then((_) => {
              this.assignDefaultValue()
              this.$refs.form.clearValidate()
              this.loaded()
            })
          } else {
            // 前台进行数据初始化
            this.$set(this.model, 'enabled', 1)
            this.$set(this.model, 'sortcode', 0)
            this.assignDefaultValue()
            this.$refs.form.clearValidate()
            this.loaded()
          }
        } else if (this.disableEditSubmit === false) {
          this.title = '编辑' + this.moduleTitle
          this.queryData(this.id).then((_) => {
            this.loaded()
          })
        } else {
          this.title = '查看' + this.moduleTitle
          this.queryData(this.id).then((_) => {
            this.$refs.form.clearValidate()
            this.loaded()
          })
        }
      })
    }
  }
}
</script>

